package cn.msn.org.controller;

import cn.msn.basic.util.AjaxResult;
import cn.msn.basic.util.PageList;
import cn.msn.org.domain.Department;
import cn.msn.org.query.DepartmentQuery;
import cn.msn.org.service.IDepartmentService;
import cn.msn.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门接口类
 */

@RestController // 等价于 Responsebody + Controller
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "查询所有" )
    @GetMapping // 等价于：@RequestMapping(     method = {RequestMethod.GET}   )
    public List<Department> findAll() {
        return departmentService.loadAll();
    }

    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id")Long id) { // 从路径中获取变量
        return departmentService.loadById(id);
    }

    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}") // 等价于：@RequestMapping(     method = {RequestMethod.Delete}   )
    @PreAuthorize(name = "部门删除", sn = "department:delete")
    public AjaxResult remove(@PathVariable("id")Long id) {
        try {
            departmentService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @ApiOperation(value = "添加或修改" )
    @PutMapping // 等价于：@RequestMapping(     method = {RequestMethod.Put}   )
    public AjaxResult delOrUpt(@RequestBody Department department) { // 从请求体【请求数据包中】中获取数据
        try {
            if (department.getId()==null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @ApiOperation(value = "分页查询" )
    @PostMapping // 等价于：@RequestMapping(     method = {RequestMethod.POST}   )
    @PreAuthorize(name = "部门分页", sn = "department:list")
    public PageList<Department> query(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    // 批量删除
    @ApiOperation(value = "批量删除接口")
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            departmentService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    // 查询部门树
    @ApiOperation(value = "查询部门树")
    @GetMapping("/departmentTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }


}
